<template>
    <div id="rules">
        <el-col :span="24" class="toolbar">
            <el-button :plain="true" type="info" @click="newRules">新增规则</el-button>
        </el-col>


        <el-table :data="moduleList" style="width: 100%" border v-loading="loading">
            <el-table-column prop="id" label="ID" width="80"></el-table-column>
            <el-table-column prop="name" label="名称"></el-table-column>
            <el-table-column prop="type" label="规则名"></el-table-column>
            <el-table-column prop="value" label="规则值"></el-table-column>
            <el-table-column label="是否开启">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.openCheck" on-text="" off-text="" @change="switchFall(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="80">
                <template slot-scope="scope">
                    <el-button type="primary" icon="edit" size="small" @click="handleEdit(scope.$index, scope.row)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--新增-->
        <!--新增-->
        <el-dialog title="新增规则" v-model="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="规则名" prop="type">
                    <el-input v-model="addForm.type" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="规则值" prop="value">
                    <el-input v-model="addForm.value" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="js" prop="js">
                    <el-input type="textarea" v-model="addForm.js" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="开启状态">
                    <el-switch v-model="addForm.openCheck" on-text="" off-text=""></el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="addSubmit('addForm')" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--编辑-->
        <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false" :before-close="close">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="editForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="规则名" prop="type">
                    <el-input v-model="editForm.type" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="规则值" prop="value">
                    <el-input v-model="editForm.value" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="js" prop="js">
                    <el-input type="textarea" v-model="editForm.js" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="editSubmit('editForm')" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import { ruleAdd,ruleList,ruleUpdate } from '../../api/api'
    export default {
        data() {
            return {
                fall: {},
                moduleList: [],
                loading: false,
                addFormVisible: false,
                addLoading: false,
                addFormRules: {
                    name: {required: true, message: '请输入名称', trigger: 'blur'},
                    type: {required: true, message: '请输入规则名', trigger: 'blur'},
                    value: {required: true, message: '请输入规则值', trigger: 'blur'},
                    openCheck: {required: true, message: '请选择', trigger: 'blur'},
                },
                addForm: {
                    name: '',
                    type: '',
                    value: '',
                    openCheck: false,
                    js: ''
                },
                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: {required: true, message: '请输入名称', trigger: 'blur'},
                    type: {required: true, message: '请输入规则名', trigger: 'blur'},
                    value: {required: true, message: '请输入规则值', trigger: 'blur'},
                    openCheck: {required: true, message: '请选择', trigger: 'blur'},
                },
                editForm: {
                    name: '',
                    type: '',
                    value: '',
                    openCheck: false,
                    js: ''
                },
            }
        },
        methods: {
            postRules(){
                let para = {};
                ruleList(para).then(res => {
                    console.log(res);
                    this.moduleList = res.data;
                })
            },
            /*页面开关状态改变*/
            switchFall(row){
                this.loading = true;
                ruleUpdate(row).then(res => {
                    console.log(res);
                    this.postRules();
                    this.loading = false;
                    this.$message({message: res.message, type: 'success'});
                })
            },
            /*打开新增窗口*/
            newRules(){
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    type: '',
                    value: '',
                    openCheck: false,
                    js: ''
                };
            },
            addSubmit(a){
                this.$refs[a].validate((valid) => {
                    if (valid){
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            let para = this.addForm;
                            ruleAdd(para).then((res) => {
                                this.addLoading = false;
                                this.$message({message: res.message, type: 'success'});
                                this.addFormVisible = false;
                                this.postRules();
                            });
                        });
                    }
                });
            },
            /*编辑*/
            handleEdit(index, row) {
                console.log(row);
                this.editFormVisible = true;
                this.editForm = row;
            },
            editSubmit(a){
                this.$refs[a].validate((valid) => {
                    if(valid){
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editFormVisible = true;
                            let para = this.editForm;
                            ruleUpdate(para).then((res) => {
                                this.editLoading = false;
                                this.$message({message: res.message, type: 'success'});
                                this.editFormVisible = false;
                                this.postRules();
                            });
                        });
                    }
                });
            },
            close(){
                this.postRules();
                this.editFormVisible = false;
            }
        },
        mounted(){
            this.postRules();
        },
    }
</script>
<style type="text/scss" lang="scss" scoped>
    #rules{}
</style>